<?php
namespace Custom\Controller;
use Think\Controller;

class TableController extends UserController
{
    function __construct()
    {
        parent::__construct();
        $this->M_table = M('Table');
        $this->M_table_cate = M('Table_cate');
    }

    /**
     * 餐桌首页
     */
    public function index()
    {
        $sid = $this->s_id;
        if($sid){
            $p = I('p');
            $page_size = '10';
            $info = $this->M_table->where('g.s_id=' . $sid)->join('as g left join sp_table_cate as k ON g.table_cate_id = k.table_cate_id')->field('g.*,k.table_cate_name')->order('g.table_num asc')->page($p, $page_size)->select();
            // p($info);
            $count = $this->M_table->where('s_id=' . $sid)->count();
            //分页
            $Page = new \Think\Page($count, $page_size);// 实例化分页类 传入总记录数和每页显示的记录数
            $show = $Page->show();// 分页显示输出
            $this->assign('show', $show);// 赋值分页输出
            $this->assign('info', $info);
        }
        $this->display();
    }

    /**
     * 添加餐桌
     */
    public function addTable()
    {
        if(!$this->s_id){
            die($this->error('请先选择门店！',U('goods/index')));
        }else{
            $list = M('Table_cate')->where('s_id='.$this->s_id)->field('table_cate_name,table_cate_id')->select();
            $this->assign('list',$list);
            if(IS_POST){
                $data['s_id'] = $this->s_id;
                $data['table_num'] =trim(I('post.tableNum'));
                $data['table_cate_id'] = trim(I('table_cate'));
                $res = $this->M_table->add($data);
                $res? $this->success('success',U('Table/index')):$this->error('failed');
            }
        }
        $this->display();
    }


    /**
     * 删除餐桌
     */
    public function delete(){
        $id = (int)$_GET['id'];
        $res = $this->M_table->delete($id);
        if($res){
            $this->ajaxReturn(array('result'=>'tp1'));
        }else{
            $this->ajaxReturn(array('result'=>'break'));
        }
    }
    /**
     * 二维码生成
     */
    public function qrcode(){
        include "./qr/phpqrcode.php";
        $tableNum = I('get.id');
        $errorLevel = "L";
        $margin = '1';
        $size = "11";
        $url = "formatkj/index/index/tableNum/".$tableNum;
        \QRcode::png($url,false,$errorLevel,$size,$margin);
    }
    /**
     *  状态修改
     */
    public function is_show(){
        
        $id = (int)$_GET['id'];
        $info = $this->M_table->find($id);
        $is_show = $info['table_status']?0:1;
        
        if ($is_show == 0) {
        //     // // S('table'.$this->c_id.'-'.$info['table_cate_id'],null);
        //     // $key = 'number'.$this->c_id.'-'.$info['table_cate_id'];
        //     // $list = S($key);
        //     // array_shift($list);
        //     // S($key,$list);
            $where['table_cate_id'] = $info['table_cate_id'];
            $where['table_status'] = 1;
            $res = (M('Table')->where($where)->count())-1;
            $key2 = 'empty'.$this->s_id.'-'.$info['table_cate_id'];
            // var_dump($res,$key2);
            S($key2,$res);
        }else {
            $where['table_cate_id'] = $info['table_cate_id'];
            $where['table_status'] = 1;
            $res = (M('Table')->where($where)->count())+1;
            $key2 = 'empty'.$this->s_id.'-'.$info['table_cate_id'];
            // var_dump($res,$key2);
            S($key2,$res);
        }
        
        $this->M_table->id = $id;
        $this->M_table->table_status = $is_show;
        $this->M_table->save();
        $is_show?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }
}